/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.data.manipulator.mutable.entity;

import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.immutable.entity.ImmutableInvulnerabilityData;
import org.spongepowered.api.data.manipulator.mutable.entity.InvulnerabilityData;
import org.spongepowered.api.data.value.mutable.MutableBoundedValue;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.common.data.ImmutableDataCachingUtil;
import org.spongepowered.common.data.manipulator.immutable.entity.ImmutableSpongeInvulnerabilityData;
import org.spongepowered.common.data.manipulator.mutable.common.AbstractData;
import org.spongepowered.common.data.value.SpongeValueFactory;
import org.spongepowered.common.data.value.mutable.SpongeValue;

public class SpongeInvulnerabilityData extends AbstractData<InvulnerabilityData, ImmutableInvulnerabilityData> implements InvulnerabilityData {

    private int invulnerabilityTicks;
    private boolean invulnerable;

    public SpongeInvulnerabilityData() {
        this(0, false);
    }

    public SpongeInvulnerabilityData(int invulnerabilityTicks, boolean invulnerable) {
        super(InvulnerabilityData.class);
        this.invulnerabilityTicks = invulnerabilityTicks;
        this.invulnerable = invulnerable;
        registerGettersAndSetters();
    }

    @Override
    public MutableBoundedValue<Integer> invulnerableTicks() {
        return SpongeValueFactory.boundedBuilder(Keys.INVULNERABILITY_TICKS)
                .minimum(0)
                .maximum(Integer.MAX_VALUE)
                .defaultValue(0)
                .actualValue(this.invulnerabilityTicks)
                .build();
    }

    @Override
    public Value<Boolean> invulnerable() {
        return new SpongeValue<>(Keys.INVULNERABLE, false, this.invulnerable);
    }

    @Override
    protected void registerGettersAndSetters() {
        registerFieldGetter(Keys.INVULNERABILITY_TICKS, () -> this.invulnerabilityTicks);
        registerFieldSetter(Keys.INVULNERABILITY_TICKS, (invulnerabilityTicks) -> this.invulnerabilityTicks = invulnerabilityTicks);
        registerKeyValue(Keys.INVULNERABILITY_TICKS, this::invulnerableTicks);

        registerFieldGetter(Keys.INVULNERABLE, () -> this.invulnerable);
        registerFieldSetter(Keys.INVULNERABLE, (invulnerable) -> this.invulnerable = invulnerable);
        registerKeyValue(Keys.INVULNERABLE, this::invulnerable);
    }

    @Override
    public InvulnerabilityData copy() {
        return new SpongeInvulnerabilityData(this.invulnerabilityTicks, this.invulnerable);
    }

    @Override
    public ImmutableInvulnerabilityData asImmutable() {
        return ImmutableDataCachingUtil.getManipulator(ImmutableSpongeInvulnerabilityData.class, this.invulnerabilityTicks, this.invulnerable);
    }

    @Override
    public DataContainer toContainer() {
        return super.toContainer()
                .set(Keys.INVULNERABILITY_TICKS, this.invulnerabilityTicks)
                .set(Keys.INVULNERABLE, this.invulnerable);
    }
}
